/* use array to  resolve Josephus problem */

/**
 * sum个身陷绝境的人围坐在一起从第一个人开始报数，报到count的人会被杀死，依次输出会被杀死的人
 * @param  {number} sum 总人数
 * @param  {number} count 报数的阈值
 */
let josephus = (sum, count) => {
    count = parseInt(count, 10);
    sum = parseInt(sum, 10);
    if (isNaN(count) || isNaN(sum)) {
        throw Error("Illegal Arguments");
    }
    let list = [];
    while (list.length < sum) {
        list.push(list.length);
    }

    let index = count - 1,
        a = null;
    while (list.length > 0) {
        if (index >= list.length) {
            index -= list.length;
        } else {
            console.log(list.splice(index, 1).toString());
            index += count - 1;
        }
    }
};

module.exports = josephus;